# Title     : TODO
# Objective : TODO
# Created by: yz
# Created on: 16/4/2021

library(ggvenn)

getDiffNames <- function() {
  fileName <- if (!file.exists("../../true_boruta_after.txt")) {
    list.files("../../", full.names = T) %>%
      keep(~str_detect(.x, "_Top")) %>%
      keep(~str_detect(.x, "Rank.csv"))
  }else {
    "../../true_boruta_after.txt"
  }
  if (length(fileName) == 0 || !file.exists(fileName)) {
    quit(status = 0)
  }
  diffData <- if (endsWith(fileName, "txt")) {
    read_tsv(fileName)
  }else {
    read_csv(fileName)
  }
  diffData %>%
    .$Metabolite
}

vennPlot <- function(datas) {

  colors <- if (size == 1) {
    c("#4040FF")
  } else if (size == 2) {
    c("#4040FF", "#FF4040")
  } else if (size == 3) {
    c("#4040FF", "#FF4040", "#00FF00")
  } else if (size == 4) {
    getClassCols(size)
  }else {
    c()
  }

  if (length(colors) == 0) {
    columnName <- str_c(treats, collapse = " & ")
    core_num <- plotData %>%
      select(c(columnName)) %>%
      filter_all(any_vars(!is.na(.))) %>%
      nrow()

    print(treats)

    otu_num <- treats %>%
      map_dbl(function(name) {
        newName <- str_c(name)
        plotData %>%
          select(newName) %>%
          filter_all(any_vars(!is.na(.))) %>%
          nrow()
      })
    fill <- getClassCols(size)
    flower_plot(treats, otu_num, core_num, start = 90, a = 0.5, b = 2, r = 1, ellipse_col = fill, circle_col = 'white')
  }else {
    tryCatch({
      p <- ggvenn(data = datas, fill_color = colors, stroke_size = 1.5, set_name_size = 12, show_percentage = F,
                  fill_alpha = .6, text_size = 8)
      print(p)
    }, error = function(e) {
      deleteAndExit(pdfFileName)
    }
    )
  }

}









